package com.neusoft.bizcore.auth.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.OrgTreeBean;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.AuthOganizationDTO;
import com.neusoft.bizcore.auth.service.AuthOganizationService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("组织管理")
@RestController
@RequestMapping(value = "/api/w/auth/organizations")
public class AuthOganizationController {

    @Autowired
    private AuthOganizationService authOganizationService;

    @ApiOperation("查询所有组织")
    @GetMapping
    public ResultListDTO<AuthOganizationDTO> index() {
        final List<AuthOganizationDTO> DTOs = this.authOganizationService.findAll();
        final ResultListDTO<AuthOganizationDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有组织")
    @GetMapping("/p")
    public PageResultDTO<AuthOganizationDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.authOganizationService.search(pageable, searchable));
    }

    @ApiOperation("创建组织")
    @PostMapping
    public ResultDTO<AuthOganizationDTO> create(@RequestBody final AuthOganizationDTO dto) {
        final AuthOganizationDTO result = this.authOganizationService.create(dto);
        return ResultDTO.success(result);
    }

    //    @ApiOperation("组织详情")
    //    @GetMapping("/{id}")
    //    public ResultDTO<AuthOganizationDTO> details(@PathVariable final Long id) {
    //        final AuthOganizationDTO dto = this.authOganizationService.details(id);
    //        return ResultDTO.success(dto);
    //    }

    @ApiOperation("组织详情")
    @GetMapping("/{code}")
    public ResultDTO<AuthOganizationDTO> details(@PathVariable final String code) {
        final AuthOganizationDTO dto;
        if (NumberUtils.isCreatable(code)) {
            dto = this.authOganizationService.details(Long.valueOf(code));
        } else {
            dto = this.authOganizationService.details(code);
        }
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除组织")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.authOganizationService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation(value = "修改组织", notes = "组织下的人有单独API维护")
    @PutMapping("/{id}")
    public ResultDTO<AuthOganizationDTO> update(@PathVariable final Long id,
            @RequestBody final AuthOganizationDTO dto) {
        final AuthOganizationDTO result = this.authOganizationService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "组织绑定用户", notes = "一次性操作全量用户。如果要单独添加、移除，调用独立API")
    @PutMapping("/{id}/bindusers")
    public ResultDTO<AuthOganizationDTO> bindUsersById(@PathVariable final Long id,
            @RequestBody final List<Long> users) {
        final AuthOganizationDTO result = this.authOganizationService.bind(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "组织添加用户", notes = "可一次性添加多个用户")
    @PutMapping("/{id}/addusers")
    public ResultDTO<AuthOganizationDTO> addUsers(@PathVariable final Long id, @RequestBody final List<Long> users) {
        final AuthOganizationDTO result = this.authOganizationService.addUsers(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "组织移除用户", notes = "可一次性移除多个用户")
    @PutMapping("/{id}/removeusers")
    public ResultDTO<AuthOganizationDTO> removeUsers(@PathVariable final Long id, @RequestBody final List<Long> users) {
        final AuthOganizationDTO result = this.authOganizationService.removeUsers(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation("获取组织树")
    @GetMapping("/tree")
    public ResultDTO<OrgTreeBean> getOrganizationTree() {
        final OrgTreeBean tree = this.authOganizationService.orgTree("root");
        return ResultDTO.success(tree);
    }

    @ApiOperation("不同所属公司组织树")
    @GetMapping("/{belong}/tree")
    public ResultDTO<OrgTreeBean> tree(@PathVariable final String belong) {
        final OrgTreeBean tree = this.authOganizationService.orgTree(belong);
        // 如果是总行的时候排除掉科技公司的组织机构
        if ("root".equals(belong)) {
            tree.getChildren().stream().forEach(l -> {
                if ("kj".equals(l.getCode())) {
                    l.setChildren(new ArrayList<>());
                }
            });
        }
        return ResultDTO.success(tree);
    }

    @ApiOperation("获取当前登录人组织树")
    @GetMapping("/currentuser/tree")
    public ResultListDTO<OrgTreeBean> getCurrentUserOrganizationTree(final UserBean user) {
        final List<OrgTreeBean> tree = this.authOganizationService.currentUserOrganizationTree(user);
        return ResultListDTO.success(tree);
    }

}
